//
//  DetailContentViewController.m
//  FruitsAndHealthy
//
//  Created by appying_wxj on 14/11/13.
//  Copyright (c) 2014年 appying_wxj. All rights reserved.
//

#import "DetailContentViewController.h"
#import "MBProgressHUD.h"
#import "UMSocialWechatHandler.h"// 微信
#import "UMSocialQQHandler.h"// qq
#import "UMSocial.h"
@interface DetailContentViewController ()

@end

@implementation DetailContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.titleLab.text = [NSString stringWithFormat:@"%@功效",self.detailTitle];
//    NSLog(@"-----水果的标题-----%@",self.detailTitle);
    self.view.backgroundColor = [UIColor colorWithRed:0.92 green:0.67 blue:0.71 alpha:1];
    _backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, kScreen_Width, kScreen_Height-64)];
    _backgroundImage.image = [UIImage imageNamed:@"image_ effectBg@2x"];
    _backgroundImage.userInteractionEnabled = YES;
    [self.view addSubview:_backgroundImage];
    // Do any additional setup after loading the view.
    
    
    // 网络请求
    if ([AllRequest checkNet]) {
        [self creatContent];
    }
    [self getDetailContentDate];
   
}
//创建控件
-(void)creatContent
{

    [self.leftButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    _titleArr = [NSMutableArray arrayWithObjects:[NSString stringWithFormat:@"%@",self.detailTitle],@"功效",@"适合人群",@"适用量",@"温馨提示", nil];
    _allArray = [NSMutableArray arrayWithObjects:self.fruitsDetailTitle,self.effectTitle,self.fitCrowdTitle,self.usageAmountTitle,self.sweetHintTitle, nil];
    
//    NSString *str = self.detailContent;//获取更新的内容
//    
//    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, kScreen_Width, kScreen_Height-64)];
//    
//    self.webView.delegate = self;
//    [self.webView loadHTMLString:str baseURL:nil];
//       self.webView.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:self.webView];
    
    _detailTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, _backgroundImage.bounds.size.width, _backgroundImage.bounds.size.height) style:UITableViewStyleGrouped];
    _detailTable.backgroundColor = [UIColor clearColor];
//    _detailTable.sectionHeaderHeight = 30;
    _detailTable.delegate = self;
    _detailTable.dataSource = self;
    _detailTable.separatorStyle = UITableViewCellSeparatorStyleNone;// 去掉表中的分割线
    [_backgroundImage addSubview:_detailTable];
    
    self.collectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [self.collectButton setBackgroundImage:[UIImage imageNamed:@"image_noCollect@2x"] forState:UIControlStateNormal];
    self.collectButton.frame = CGRectMake(kScreen_Width-70, kScreen_Height-230, 50, 50);
    [_backgroundImage addSubview:self.collectButton];
    
    self.shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.shareButton setBackgroundImage:[UIImage imageNamed:@"image_share@2x"] forState:UIControlStateNormal];
    self.shareButton.frame = CGRectMake(kScreen_Width-70, kScreen_Height-160, 50, 50);
    [self.shareButton addTarget:self action:@selector(shareButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.shareButton.showsTouchWhenHighlighted = YES;// 按钮按下时会发光
    [_backgroundImage addSubview:self.shareButton];


    
}
#pragma mark----------------------UITableView  dataSource  delegate---------------------

// 设置多少区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 5;
}

//// 区头标题
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    return [_titleArr objectAtIndex:section];
//}
#pragma mark- 自定义区头的view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, 30)];
    view.backgroundColor = [UIColor clearColor];
    
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.width, view.bounds.size.height)];
    bgImageView.image = [UIImage imageNamed:@"image_effect@2x"];
    bgImageView.userInteractionEnabled = YES;
    [view addSubview:bgImageView];
    
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(32, 5, 120, 20)];
    titleLab.text = [_titleArr objectAtIndex:section];
    titleLab.textColor = [UIColor colorWithRed:0.91 green:0.64 blue:0.15 alpha:1];
    titleLab.font = [UIFont systemFontOfSize:16];
    titleLab.textAlignment = NSTextAlignmentLeft;
    [bgImageView addSubview:titleLab];
    
    return view;
}
//默认高度20  设置区头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
// 每个区返回多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
// 动态改变cell的高度  行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    
    switch (indexPath.section) {
        case 0:{
            _ContentStr = [_allArray objectAtIndex:0];
        }
            break;
        case 1:{
            _ContentStr = [_allArray objectAtIndex:1];
        }
            
            break;
        case 2:{
            _ContentStr = [_allArray objectAtIndex:2];
        }
            
            break;
        case 3:{
            _ContentStr = [_allArray objectAtIndex:3];
        }
            
            break;
        case 4:{
            _ContentStr = [_allArray objectAtIndex:4];
        }
            
            break;
            
            
        default:
            break;
            
    }
    CGSize stringSize = [_ContentStr sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(kScreen_Width-20, 10000) lineBreakMode:NSLineBreakByCharWrapping];
    float dataHeight = stringSize.height;
    return dataHeight+20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static  NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil)
    {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    } else{
        while ([cell.contentView.subviews lastObject]!=nil) {
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    
    //    设置单元格选择样式
    cell.selectionStyle= UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    
    _contentLabel = [[UILabel alloc] init];
//    _contentLabel.frame = CGRectMake(10, 5,  kScreen_Width-20, dataHeight);
    _contentLabel.numberOfLines = 0;
   
    _contentLabel.backgroundColor = [UIColor clearColor];
    _contentLabel.font = [UIFont systemFontOfSize:12];
    [cell.contentView addSubview:_contentLabel];
    
    switch (indexPath.section) {
        case 0:{
//            _ContentStr = [_allArray objectAtIndex:0];
            NSString *allText = self.fruitsDetailTitle;
            CGSize stringSize = [allText sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(kScreen_Width-20, 10000) lineBreakMode:NSLineBreakByCharWrapping];
            float dataHeight = stringSize.height;
            _contentLabel.text = allText;
            _contentLabel.frame = CGRectMake(10, 5,  kScreen_Width-20, dataHeight);
        }
            break;
        case 1:{
            NSString *allText = self.effectTitle;
            CGSize stringSize = [allText sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(kScreen_Width-20, 10000) lineBreakMode:NSLineBreakByCharWrapping];
            float dataHeight = stringSize.height;
            _contentLabel.text = allText;
            _contentLabel.frame = CGRectMake(10, 5,  kScreen_Width-20, dataHeight);
        }
            
            break;
        case 2:{
            NSString *allText = self.fitCrowdTitle;
            CGSize stringSize = [allText sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(kScreen_Width-20, 10000) lineBreakMode:NSLineBreakByCharWrapping];
            float dataHeight = stringSize.height;
            _contentLabel.text = allText;
            _contentLabel.frame = CGRectMake(10, 5,  kScreen_Width-20, dataHeight);
        }
            
            break;
        case 3:{
            NSString *allText = self.usageAmountTitle;
            CGSize stringSize = [allText sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(kScreen_Width-20, 10000) lineBreakMode:NSLineBreakByCharWrapping];
            float dataHeight = stringSize.height;
            _contentLabel.text = allText;
            _contentLabel.frame = CGRectMake(10, 5,  kScreen_Width-20, dataHeight);
        }
            
            break;
        case 4:{
            NSString *allText = self.sweetHintTitle;
            CGSize stringSize = [allText sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(kScreen_Width-20, 10000) lineBreakMode:NSLineBreakByCharWrapping];
            float dataHeight = stringSize.height;
            _contentLabel.text = allText;
            _contentLabel.frame = CGRectMake(10, 5,  kScreen_Width-20, dataHeight);
        }
            
            break;
            
            
        default:
            break;
            
    }


    return cell;
}



//- (void)webViewDidFinishLoad:(UIWebView *)webView{
//    // 字体的颜色的设置
//    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'Yacyan'"];
//
//}
// 获取收藏数据
-(void)getDetailContentDate{
    _nameArray = [[NSMutableArray alloc] init];
    _contentArray = [[NSMutableArray alloc] init];
    // 创建表 FMDB数据存储
    [BaseFruits createTable];
    _array = [BaseFruits getAllFruitsTitle];
    if ([self.array count] == 0) {
        [self.collectButton setBackgroundImage:[UIImage imageNamed:@"image_noCollect@2x"] forState:UIControlStateNormal];
        [self.collectButton addTarget:self action:@selector(detailContentCollectButtonClick:)
                   forControlEvents:UIControlEventTouchUpInside];
    }else{
        for (int i = 0 ; i < self.array.count; i++) {
            [_nameArray addObject:[_array objectAtIndex:i]];
        }
        NSMutableArray *nameArr= [[NSMutableArray alloc] initWithCapacity:0];
        for (int j = 0 ; j < _nameArray.count; j++) {
            _name = [_nameArray objectAtIndex:j];
            _nameStr= [NSString stringWithFormat:@"%@",_name.fruitsTitle];
            [nameArr addObject:_nameStr];
        }
        for (int g = 0; g < nameArr.count; g++) {
//            NSLog(@"----------获取到的数据-------%lu",(unsigned long)nameArr.count);
            // 右边按钮的点击事件
            if ([nameArr containsObject:self.titleLab.text]) {
                [self.collectButton setBackgroundImage:[UIImage imageNamed:@"image_finishCollect@2x"] forState:UIControlStateNormal];
                self.collectButton.enabled = NO;
            }else{
                [self.collectButton setBackgroundImage:[UIImage imageNamed:@"image_noCollect@2x"] forState:UIControlStateNormal];
                [self.collectButton addTarget:self action:@selector(detailContentCollectButtonClick:)
                           forControlEvents:UIControlEventTouchUpInside];
            }
        }

    }

}

#pragma mark------ButtonClick-------
-(void)btnClick:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
// 收藏按钮
-(void)detailContentCollectButtonClick:(UIButton *)sender{
    [self.collectButton setBackgroundImage:[UIImage imageNamed:@"image_finishCollect@2x"] forState:UIControlStateNormal];
    Fruits *title = [[Fruits alloc] init];
    title.fruitsTitle = self.titleLab.text;
    title.fruitsContent = self.detailContent;
    [BaseFruits insertFruits:title];
    // 每次插入之后从数据库中获取所有数据
    self.array = [BaseFruits getAllFruitsTitle];
    
    self.collectButton.enabled = NO;

}
-(void)shareButtonClick:(UIButton *)sender{
    [self.shareButton setHidden:YES];
    [self.collectButton setHidden:YES];
    
    //状态提示框
    _greyView = [[UIView alloc]initWithFrame:
                 CGRectMake(0, 0, Kwidth, kheight)];
    _greyView.backgroundColor =
    [UIColor colorWithRed:70.0 / 255.0 green:70.0 / 255.0 blue:70.0 / 255.0 alpha:0.5];
    [self.view addSubview:_greyView];
    
    UIView *alertView = [[UIView alloc]initWithFrame:CGRectMake(10, kheight, 300, 100)];
    alertView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:0.8];
    [alertView.layer setMasksToBounds:YES];
    [alertView.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
    [alertView.layer setBorderWidth:1.0];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 0 });
    [alertView.layer setBorderColor:colorref];//边框颜色
    
    //qq按钮
    UIButton *qqButton = [UIButton buttonWithType:UIButtonTypeCustom];
    qqButton.frame = CGRectMake(35, 30, 40, 40);
    [qqButton setImage:[UIImage imageNamed:@"image_QQ.png"] forState:UIControlStateNormal];
    [qqButton addTarget:self action:@selector(qqButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    qqButton.tag = 100;
    [alertView addSubview:qqButton];
    
    //微信按钮
    UIButton *weiXinButton = [UIButton buttonWithType:UIButtonTypeCustom];
    weiXinButton.frame = CGRectMake(100, 30, 40, 40);
    [weiXinButton setImage:[UIImage imageNamed:@"sns_icon_22.png"] forState:UIControlStateNormal];
    [weiXinButton addTarget:self action:@selector(weiXinButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    weiXinButton.tag = 101;
    [alertView addSubview:weiXinButton];
    
    //  取消的按钮
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelButton.frame = CGRectMake(10, kheight+120, 300, 40);
    [cancelButton.layer setMasksToBounds:YES];
    [cancelButton.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
    [cancelButton.layer setBorderWidth:1.0];
    [cancelButton.layer setBorderColor:colorref];//边框颜色
    cancelButton.backgroundColor = [UIColor whiteColor];
    [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    cancelButton.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:0.8];
    [cancelButton addTarget:self action:@selector(backViewController:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    [UIView animateWithDuration:0.5 animations:^{
        alertView.frame=CGRectMake(10, kheight-170, 300, 100);
        cancelButton.frame = CGRectMake(10, kheight-60, 300, 40);
        [_greyView addSubview:cancelButton];
        [_greyView addSubview:alertView];
    }completion:^(BOOL finished) {
    }];
    

}
// qq分享
-(void)qqButtonClick:(UIButton *)sender{
    [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQQ] content:@"《水果益健康》教你吃水果，巧吃水果萌萌哒！都是吃货无须解释！你倒是点我呀！https://fruithealthy.avosapps.com/share" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
        if (response.responseCode == UMSResponseCodeSuccess) {
           // NSLog(@"分享成功！");
        }
    }];
}
// 微信分享
-(void)weiXinButtonClick:(UIButton *)sender{
    [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:@"《水果益健康》教你吃水果，巧吃水果萌萌哒！都是吃货无须解释！你倒是点我呀！https://fruithealthy.avosapps.com/share" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
        if (response.responseCode == UMSResponseCodeSuccess) {
            //NSLog(@"分享成功！");
        }
    }];
}
// 分享页面的取消按钮
-(void)backViewController:(UIButton *)sender{
    [self.collectButton setHidden:NO];
    [self.shareButton setHidden:NO];
    [_greyView removeFromSuperview];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
